package com.yuezhi.mylauncher.utils;

import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Toast;

import com.yuezhi.mylauncher.base.BaseApplication;

/**
 * Toast工具类
 *
 * @author Bai Yanchao
 * @since  2015/3/17
 */
public class ToastUtil {

    private static final Handler handler = new Handler(Looper.getMainLooper());
    private static Toast toast = null;

    public static void showMessage(final String msg) {
        showToastMessage(msg);
    }

    /**
     * 显示一个文本并且设置时长
     */
    public static void showToastMessage(final CharSequence msg) {
        if (msg == null || msg.equals("")) {
            return;
        }
        handler.post(() -> {
            synchronized (ToastUtil.class) {
                if (toast==null) {
                    toast = Toast.makeText(BaseApplication.getContext(), msg, Toast.LENGTH_SHORT);
                }else{
                    View view = toast.getView();
                    toast.cancel();
                    toast= new Toast(BaseApplication.getContext());
                    toast.setView(view);
                    toast.setDuration(Toast.LENGTH_SHORT);
                    toast.setText(msg);
                }
                toast.show();
            }
        });
    }
}
